package com.etime.shycourse.controller.salers;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.dto.SalerDto;
import com.etime.shycourse.dto.SysUserDto;
import com.etime.shycourse.dto.UserSalerDto;
import com.etime.shycourse.orde.pojo.Orders;
import com.etime.shycourse.pojo.SysUser;
import com.etime.shycourse.services.salers.interfaces.SalerServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 销售人员管理 前端控制器
 * </p>
 *
 * @author wb
 * @since 2022-03-24
 */
@Api(tags = "后台-销售人员")
@RestController
@RequestMapping("/sys/saler")
public class SalerController {
    @Autowired
    private SalerServices service;

    @ApiOperation("修改用户、销售信息。返回int 成功 =1 失败=0 ")
    @PostMapping("/upUserSaler")
    public BaseMessage updateUserSalers(UserSalerDto dto) {
        BaseMessage msg = new BaseMessage();
        int i = service.updateUserSaler(dto);
        if (i > 0) {
            msg.setCode(i);
            msg.setMsg("修改成功");
        } else {
            msg.setCode(0);
            msg.setMsg("修改失敗");
        }
        return msg;
    }

    @ApiOperation("让用户成为销售人员。返回int 成功 =1 失败=0 ")
    @PostMapping("/upUser/inset")
    public BaseMessage updateUserBecomeSaler(UserSalerDto dto) {
        BaseMessage msg = new BaseMessage();
        int i = service.updateUserBecomeSaler(dto);
        if (i > 0) {
            msg.setCode(i);
            msg.setMsg("修改成功");
        } else {
            msg.setCode(0);
            msg.setMsg("修改失敗");
        }
        return msg;
    }

    @ApiOperation("根据用id 查询用户有销售人员的信息。返回int 成功 =1 失败=0 ")
    @GetMapping("/get/{userId}")
    public PojoMessage selectUserSaler(@PathVariable Integer userId) {
        PojoMessage msg = new PojoMessage();
        UserSalerDto data = service.selectUserSaler(userId);
        if (data != null) {
            msg.setCode(1);
            msg.setData(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有数据");
        }
        return msg;
    }

    @ApiOperation("销售人员管理 分頁")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行"),
            @ApiImplicitParam(name = "deptid", value = "部门id"),
            @ApiImplicitParam(name = "salerauditstate", value = "状态"),
            @ApiImplicitParam(name = "usertruename", value = "用户名 模糊"),
    })
    @GetMapping("/page")
    public PageMessage<SalerDto> selectSalerPage(SalerDto salerDto, Long pageNum, Long pageSize) {
        PageMessage<SalerDto> msg = new PageMessage<>();
        Page<SalerDto> data = service.selectSalerPage(salerDto, pageNum, pageSize);
        if (data.getSize() > 0) {
            msg.setCode(1);
            msg.setPagedata(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("售人员对应所有的客户查询 分頁")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少行"),
            @ApiImplicitParam(name = "userid", value = "用户id"),
            @ApiImplicitParam(name = "nickname", value = "用户姓名 微信名称模糊查询"),
            @ApiImplicitParam(name = "one", value = "开始时间 格式:yyyy-MM-dd"),
            @ApiImplicitParam(name = "two", value = "结束时间 格式:yyyy-MM-dd"),
    })
    @GetMapping("/client/page")
    public PageMessage<SysUser> selectSaleClientPage(SysUserDto user, Long pageNum, Long pageSize) {
        PageMessage<SysUser> msg = new PageMessage<>();
        Page<SysUser> data = service.selectSaleClientPage(user, pageNum, pageSize);
        if (data.getSize() > 0) {
            msg.setCode(1);
            msg.setPagedata(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("销售人员查询自己的客户 分頁")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少行"),
    })
    @GetMapping("/client/my/page")
    public PageMessage<SysUser> selectSaleMyClientPage(HttpServletRequest request, SysUserDto user, Long pageNum, Long pageSize) {
        PageMessage<SysUser> msg = new PageMessage<>();
        String token = request.getHeader("token");
        if (token == null) {
            msg.setCode(-2);
            msg.setMsg("未获取到用户信息；可能登录超时或者非法访问");
        } else {
            Page<SysUser> data = service.selectSaleMyClientPage(token, user, pageNum, pageSize);
            if (data != null && data.getSize() > 0) {
                msg.setCode(1);
                msg.setPagedata(data);
            } else {
                msg.setCode(0);
                msg.setMsg("没有查询到数据");
            }
        }
        return msg;
    }

    @ApiOperation("查询指定客户的消费记录 分頁")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条数据"),
            @ApiImplicitParam(name = "orderuserid", value = "用户id"),
            // @ApiImplicitParam(name = "orderstate", value = "订单状态"),
            // @ApiImplicitParam(name = "ordercreatetimeOne", value = "可选填选择条件用户下单时间 开始"),
            // @ApiImplicitParam(name = "ordercreatetimeTwo", value = "可选填选择条件用户下单时间 结束"),
    })
    @GetMapping("/client/order/page")
    public PageMessage<Orders> selectOrderListPageByClient(Orders orders, Long pageNum, Long pageSize) {
        PageMessage<Orders> msg = new PageMessage<>();
        Page<Orders> data = service.selectOrderListPageByClient(orders, pageNum, pageSize);
        if (data != null && data.getSize() > 0) {
            msg.setCode(1);
            msg.setPagedata(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }
}
